//
//  LGLineChartViewController.m
//  LGCharts
//
//  Created by lg on 2019/5/16.
//  Copyright © 2019 lg. All rights reserved.
//

#import "LGLineChartViewController.h"

@interface LGLineChartViewController () <ChartViewDelegate>

@property (nonatomic, strong) LineChartView *lineChartView;

@end

@implementation LGLineChartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"LGLineChartViewController";
    self.view.backgroundColor = UIColor.whiteColor;
    
    //添加折线视图LineChartView
    self.lineChartView = [[LineChartView alloc] initWithFrame:CGRectMake(20, 200, [UIScreen mainScreen].bounds.size.width-40, 300)];
    self.lineChartView.delegate = self;//设置代理
    [self.view addSubview:self.lineChartView];
    
    //基本样式
    self.lineChartView.backgroundColor =  [UIColor colorWithRed:230/255.0f green:253/255.0f blue:253/255.0f alpha:1];
    self.lineChartView.noDataText = @"暂无数据";
    //交互样式
    self.lineChartView.scaleYEnabled = NO;//取消Y轴缩放
    self.lineChartView.doubleTapToZoomEnabled = NO;//取消双击缩放
//    self.lineChartView.dragEnabled = YES;//启用拖拽图标
//    self.lineChartView.dragDecelerationEnabled = YES;//拖拽后是否有惯性效果
//    self.lineChartView.dragDecelerationFrictionCoef = 0.9;//拖拽后惯性效果的摩擦系数(0~1)，数值越小，惯性越不明显
    //X轴样式
    ChartXAxis *xAxis = self.lineChartView.xAxis;
    xAxis.axisLineWidth = 1.0/[UIScreen mainScreen].scale;//设置X轴线宽
    xAxis.labelPosition = XAxisLabelPositionBottom;//X轴的显示位置，默认是显示在上面的
    xAxis.drawGridLinesEnabled = YES;//是否绘制网格线
    xAxis.labelTextColor = HexColor(0x057748);//label文字颜色
    xAxis.axisMinimum = 0;//设置x轴最小值
    xAxis.axisLineColor = UIColor.clearColor;//设置x轴颜色
    xAxis.labelCount = 10;//x轴label个数
    //Y轴样式
    self.lineChartView.rightAxis.enabled = NO;//不绘制右边轴
    ChartYAxis *leftAxis = self.lineChartView.leftAxis;//获取左边Y轴
    leftAxis.enabled = NO;//是否显示Y轴
//    leftAxis.labelCount = 5;//Y轴label数量，数值不一定，如果forceLabelsEnabled等于YES, 则强制绘制制定数量的label, 但是可能不平均
//    leftAxis.forceLabelsEnabled = NO;//不强制绘制指定数量的label
//    leftAxis.axisMinimum = 0;//设置Y轴的最小值
//    leftAxis.drawZeroLineEnabled = YES;//从0开始绘制
//    leftAxis.axisMaximum = 105;//设置Y轴的最大值
//    leftAxis.inverted = NO;//是否将Y轴进行上下翻转
//    leftAxis.axisLineWidth = 1.0/[UIScreen mainScreen].scale;//Y轴线宽
//    leftAxis.axisLineColor = [UIColor blackColor];//Y轴颜色
//    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
//    leftAxis.labelTextColor = HexColor(0x057748);//文字颜色
//    leftAxis.labelFont = [UIFont systemFontOfSize:10.0f];//文字字体
//    //网格线样式
//    leftAxis.drawGridLinesEnabled = NO;//是否绘制网格线
//    leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];//设置虚线样式的网格线
//    leftAxis.gridColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//网格线颜色
//    leftAxis.gridAntialiasEnabled = YES;//开启抗锯齿
    //添加限制线
//    ChartLimitLine *limitLine = [[ChartLimitLine alloc] initWithLimit:80 label:@"限制线"];
//    limitLine.lineWidth = 2;
//    limitLine.lineColor = [UIColor greenColor];
//    limitLine.lineDashLengths = @[@5.0f, @5.0f];//虚线样式
//    limitLine.labelPosition = ChartLimitLabelPositionRightTop;//位置
//    limitLine.valueTextColor = HexColor(0x057748);//label文字颜色
//    limitLine.valueFont = [UIFont systemFontOfSize:12];//label字体
//    [leftAxis addLimitLine:limitLine];//添加到Y轴上
//    leftAxis.drawLimitLinesBehindDataEnabled = YES;//设置限制线绘制在折线图的后面
    //描述及图例样式
//    self.lineChartView.chartDescription.text = @"折线图";
//    self.lineChartView.chartDescription.textColor = UIColor.darkGrayColor;
    
//    self.lineChartView.legend.form = ChartLegendFormLine;
//    self.lineChartView.legend.formSize = 30;
//    self.lineChartView.legend.textColor = [UIColor darkGrayColor];
    self.lineChartView.legend.enabled = NO;//是否显示图例
    
    self.lineChartView.data = self.lineData;
    [self.lineChartView animateWithXAxisDuration:1.0f];
}

- (id)lineData {
    NSMutableArray *values = [[NSMutableArray alloc] init];
    
    for (int i = 0; i <= 10; i++)
    {
        double val = arc4random_uniform(100) + 3;
        [values addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
    }
    
    LineChartDataSet *dataSet = [[LineChartDataSet alloc] initWithValues:values];
    dataSet.lineWidth = 2.0f;//折线宽度
    dataSet.drawValuesEnabled = YES;//是否在拐点处显示数据
    dataSet.valueColors = @[[UIColor blackColor]];//折线拐点处显示数据的颜色
    dataSet.drawCirclesEnabled = YES;//是否绘制拐点
    dataSet.cubicIntensity = 0.2;// 曲线弧度
    dataSet.circleRadius = 3.0f;//拐点半径
    dataSet.mode = LineChartModeCubicBezier;// 模式为曲线模式
    dataSet.axisDependency = AxisDependencyLeft;
    dataSet.circleHoleRadius = 2.0f;//空心的半径
    dataSet.circleColors = @[UIColor.blueColor];//空心的圈的颜色
    dataSet.circleHoleColor = UIColor.whiteColor;//空心的颜色
    dataSet.highlightEnabled = YES;//选中拐点,是否开启高亮效果(显示十字线)
    dataSet.highlightColor = [UIColor clearColor];
    dataSet.valueFont = [UIFont systemFontOfSize:12];
    dataSet.drawFilledEnabled = YES;//是否填充颜色
    // 设置渐变效果
    [dataSet setColor:[UIColor colorWithRed:0.114 green:0.812 blue:1.000 alpha:1.000]];//折线颜色
    NSArray *gradientColors = @[(id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor,
                                (id)[ChartColorTemplates colorFromString:@"#ff00ff00"].CGColor];
    CGGradientRef gradientRef = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
    dataSet.fillAlpha = 1.0f;//透明度
    dataSet.fill = [ChartFill fillWithLinearGradient:gradientRef angle:90.0f];//赋值填充颜色对象
    CGGradientRelease(gradientRef);//释放gradientRef
    // 把线放到LineChartData里面,因为只有一条线，所以集合里面放一个就好了，多条线就需要不同的 set 啦
    
    LineChartData *data = [[LineChartData alloc] initWithDataSets:@[dataSet]];
    
    return data;
}

@end
